<?
//------------------------------------------------------------------------------
function set_seq ($seq, $pr_id) {
	GLOBAL $id, $dbh81;
        $sql['type']    = "UPDATE";
        $sql['params']  = " seq='$seq' ";
        $sql['table']   = "products";
        $sql['where']   = "reseller_id='$id' AND product_id='$pr_id' ";
        $sql['options'] = "";
        $sql['return']  = "";
        $result =  sql_exec($sql,$dbh81);
        return($result);
}
//------------------------------------------------------------------------------
function get_id_for_seq($seq) {
	GLOBAL $id, $dbh81;
        $sql['type']    = "SELECT";
        $sql['params']  = "product_id";
        $sql['table']   = "products";
        $sql['where']   = "reseller_id='$id' AND seq='$seq' AND type='plan'";
        $sql['options'] = "";
        $sql['return']  = "";
        if (!$res = sql_exec($sql,$dbh81)) {
		exit("ERROR1");
	} else {
        return($res[0]['product_id']);
       }
}
//------------------------------------------------------------------------------
function get_lower_seq($seq) {
	GLOBAL $id, $dbh81;
        $sql['type']    = "SELECT";
        $sql['params']  = "seq";
        $sql['table']   = "products";
        $sql['where']   = "reseller_id='$id' AND type='plan' AND seq < '$seq' ORDER BY seq DESC LIMIT 1";
        $sql['options'] = "";
        $sql['return']  = "";
        if (!$res = sql_exec($sql,$dbh81)) {
		exit("ERROR1");
	} else {
        return($res[0]['seq']);
       }
}
//------------------------------------------------------------------------------
function get_higher_seq($seq) {
	GLOBAL $id, $dbh81;
        $sql['type']    = "SELECT";
        $sql['params']  = "seq";
        $sql['table']   = "products";
        $sql['where']   = "reseller_id='$id' AND type='plan' AND seq > '$seq' ORDER BY seq LIMIT 1";
        $sql['options'] = "";
        $sql['return']  = "";
        if (!$res = sql_exec($sql,$dbh81)) {
		exit("ERROR1");
	} else {
        return($res[0]['seq']);
       }
}
//-------------------------------------------------------------------------------
switch ($move_pl) {
	case 'up':
		$new_seq = get_lower_seq($pl_cur_seq);
		if ($new_seq>0) {
			$prod_id2 = get_id_for_seq($new_seq);
			echo "prod_id2 => $prod_id2";
			set_seq($new_seq, $prod_id);
			set_seq($pl_cur_seq, $prod_id2);
		}
		break;
	case 'down':
	echo "debug5<br>";
		$new_seq = get_higher_seq($pl_cur_seq);
                if ($new_seq <= $pl_min_seq) {
                        $prod_id2 = get_id_for_seq($new_seq);
                        set_seq($new_seq, $prod_id);
                        set_seq($pl_cur_seq, $prod_id2);
		}
		break;
}
		$params[sid]=$sid;
		$params[action]='managePlans';
		$gurl = "$baseURL/$webDIR/?".encode_params($params);
		echo "<script language='JavaScript'>
		document.location='$gurl';
		</script>";
?>
